using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Hmw;
using net_work.Data.Enum.Hmw;

namespace net_work.Config.DataBaseConfig.Hmw;

/// <summary>
/// 题目答案提交实体配置类
/// </summary>
public class QuestionAnswerSubmissionEntityTypeConfiguration : IEntityTypeConfiguration<QuestionAnswerSubmission>
{
    public void Configure(EntityTypeBuilder<QuestionAnswerSubmission> builder)
    {
        builder.ToTable("hmw_ques_ans_sbt");

        builder.HasKey(qas => qas.Id);

        // 配置字段名映射
        builder.Property(qas => qas.Id).HasColumnName("ques_ans_sbt_id");
        builder.Property(qas => qas.Content).HasColumnName("ques_ans_sbt_cnt").HasMaxLength(2048);
        builder.Property(qas => qas.Score).HasColumnName("ques_ans_score");

        // 将 SubmissionStatus 枚举存储为字符串
        builder.Property(qas => qas.Status)
            .HasColumnName("ques_ans_sbt_status")
            .IsRequired()
            .HasDefaultValue(SubmissionStatus.No)
            .HasMaxLength(64)
            .HasConversion(
                v => v.ToString(), // 枚举转字符串
                v => (SubmissionStatus)Enum.Parse(typeof(SubmissionStatus), v) // 字符串转枚举
            );

        builder.Property(qas => qas.FirstSubmissionTime).HasColumnName("ques_ans_sbt_first_datetime");
        builder.Property(qas => qas.LastSubmissionTime).HasColumnName("ques_ans_sbt_last_datetime");
        builder.Property(qas => qas.HomeworkQuestionId).HasColumnName("hmw_ques_id").IsRequired();
        builder.Property(qas => qas.StudentId).HasColumnName("stu_id").IsRequired();

        builder.Property(qas => qas.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(qas => qas.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(qas => qas.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(qas => qas.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(qas => qas.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(qas => qas.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}